from email.policy import default
import uuid
from django.db import models

"""
Django 数据库迁移命令: 
python manage.py makemigrations
python manage.py migrate
.venv\Scripts\activate
"""


# 用户系统
class User(models.Model):
    uid = models.UUIDField(primary_key=True, verbose_name='用户ID')
    username = models.CharField(max_length=255,verbose_name='用户名')

# 记账分类
class Category(models.Model):
    uid = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="创建者ID", null=False, blank=False)
    cid = models.UUIDField(primary_key=True, verbose_name='分类ID')
    name = models.CharField(max_length=255, verbose_name='分类名称')
    
    class Meta:
        unique_together = ('uid', 'name')  # 确保同一用户下分类名称唯一

# 记账系统
class Account(models.Model):
    aid = models.UUIDField(primary_key=True, verbose_name='记账ID')
    uid = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户ID')
    amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='金额')
    category = models.ForeignKey(Category, on_delete=models.CASCADE, verbose_name='分类')
    description = models.TextField(verbose_name='描述', null=True, blank=True)
    # 记账类型，1：收入，2：支出
    a_type = models.IntegerField(verbose_name='记账类型', choices=[(1, '收入'), (2, '支出')])
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

# 记账图片系统
class AccountImage(models.Model):
    iid = models.UUIDField(primary_key=True, verbose_name='图片ID')
    account = models.ForeignKey(Account, on_delete=models.CASCADE, verbose_name='记账记录', related_name='images')
    # 图片存储路径
    image = models.FileField(upload_to='account_images/%Y/%m/%d/', verbose_name='图片文件')
    description = models.CharField(max_length=255, verbose_name='图片描述', null=True, blank=True)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    def __str__(self):
        return f"Image {self.iid} for Account {self.account.aid}"